package com.zyzd.pathdemo.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.EmbossMaskFilter;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import com.zyzd.pathdemo.R;

/**
 * Created by 李宗源 on 2017/5/4.
 */

public class MaskFilterView extends View {
    private Paint paint;
    private TextPaint mTextPaint;
    private BlurMaskFilter bmf1;
    private BlurMaskFilter bmf2;
    private BlurMaskFilter bmf3;
    private BlurMaskFilter bmf4;
    private EmbossMaskFilter emboss;

    public MaskFilterView(Context context) {
        this(context, null);
    }

    public MaskFilterView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MaskFilterView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
//        initOfBlurMask();
        initOfEmbossMask();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//        onDrawOfBlurMask(canvas);
        onDrawOfEmbossMask(canvas);
    }

    private void initOfBlurMask() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setTextSize(64);
        mTextPaint.setColor(Color.BLUE);
        mTextPaint.setTextAlign(Paint.Align.CENTER);

        bmf1 = new BlurMaskFilter(100, BlurMaskFilter.Blur.NORMAL);
        bmf2 = new BlurMaskFilter(100, BlurMaskFilter.Blur.SOLID);
        bmf3 = new BlurMaskFilter(100, BlurMaskFilter.Blur.OUTER);
        bmf4 = new BlurMaskFilter(100, BlurMaskFilter.Blur.INNER);
    }

    private void initOfEmbossMask() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(32);
        paint.setTextSize(220);
        float[] direction = new float[]{1, 1, 1};
        // 设置环境光亮度
        float light = 0.1f;
        // 选择要应用的反射等级
        float specular = 8;
        // 向mask应用一定级别的模糊
        float blur = 3;
        emboss = new EmbossMaskFilter(direction, light, specular, blur);
    }

    private void onDrawOfBlurMask(Canvas canvas) {
        canvas.translate(0, 200);
        canvas.drawColor(Color.WHITE);
        paint.setMaskFilter(bmf1);
        canvas.drawCircle(300, 300, 150, paint);
        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_physician_team), 0, 0, paint);
        canvas.drawText("NORMAL", 300, 600, mTextPaint);

        paint.setMaskFilter(bmf2);
        canvas.drawCircle(300, 900, 150, paint);
        canvas.drawText("SOLID", 300, 1200, mTextPaint);

        paint.setMaskFilter(bmf3);
        canvas.drawCircle(800, 300, 150, paint);
        canvas.drawText("OUTER", 800, 600, mTextPaint);

        paint.setMaskFilter(bmf4);
        canvas.drawCircle(800, 900, 150, paint);
        canvas.drawText("INNER", 800, 1200, mTextPaint);
    }


    private void onDrawOfEmbossMask(Canvas canvas) {
        canvas.drawText("山大王", 30, 400, paint);
    }

    /**
     * 其中direction表示x,y,z的三个方向
     *
     * @param x
     * @param y
     * @param z
     * @param light    ambient表示的是光的强度，范围为[0-1]
     * @param specular specular是反射亮度的系数
     * @param blur     blurRadius是指模糊的半径
     */
    @SuppressLint("NewApi")
    public void setparam(float x, float y, float z, float light,
                         float specular, float blur) {
        emboss = new EmbossMaskFilter(new float[]{x, y, z}, light, specular,
                blur);
        paint.setMaskFilter(emboss);
        invalidate();
    }
}
